package org.example.forum.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.confing.AppConfig;
import org.example.forum.model.Article;
import org.example.forum.model.Board;
import org.example.forum.model.User;
import org.example.forum.services.IArticleService;
import org.example.forum.services.IBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
@Tag(name = "文章接口")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IBoardService boardService;
    @Operation(summary = "发布文章")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request, @Parameter(description = "板块id") @RequestParam(value = "boardId") @NonNull Long boardId,
                            @Parameter(description ="文章标题") @RequestParam (value = "title") @NonNull String title,
                            @Parameter(description = "文章内容") @RequestParam(value = "content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验板块
        Board board = boardService.selectById(boardId.longValue());
        if(board==null||board.getDeleteState()==1||board.getState()==1){
            log.warn(ResultCode.FAILED_BOARD_BANNEO.toString());
            return AppResult.failed(ResultCode.FAILED_CREATE);
        }
        //封装文章对象
        Article article=new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);//板块id
        article.setUserId(user.getId());//作者id
        //调用service
        articleService.create(article);
        return AppResult.success(ResultCode.SUCCESS);
    }

    @GetMapping("/getAllBoardId")
    @Operation(summary = "获取帖子列表")
    public AppResult<List<Article>> getAllByBoard(@Parameter(description = "板块id") @RequestParam (value = "boardId" ,required = false) Long boardId){
        List<Article> articles;
        if(boardId==null){
            articles = articleService.selectAll();
        }else{
            articles = articleService.selectAllByBoardId(boardId);
        }
        if(articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }

    @Operation(summary = "根据帖子id获得详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,@Parameter(description = "帖子id") @RequestParam(value = "id") @NonNull Long id) {
        Article article = articleService.selectDetialById(id);
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (article == null) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if (user.getId() == article.getUserId()) {
            article.setIsOwn(true);
        }
        return AppResult.success(article);
    }
    @PostMapping("/modify")
    @Operation(summary = "修改帖子")
    public AppResult modify(@Parameter(description = "帖子id") @RequestParam(value = "id") @NonNull Long id,
                            @Parameter(description = "帖子标题") @RequestParam(value = "title") @NonNull String title,
                            @Parameter(description = "帖子内容") @RequestParam(value = "content") @NonNull String content,
                            HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        //判断用户状态
        if(user.getState()==1){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子通过帖子id
        Article article = articleService.selectById(id);
        //判断帖子是否有效
        if(article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断用户是不是作者
        if(article.getUserId()!=user.getId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //判断帖子的状态
        if(article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNEO);
        }
        //调用service
        articleService.modify(id,title,content);
        log.info("帖子更新成功.Article id="+id+"User id="+user.getId());
        //返回正确的信息
        return AppResult.success();
    }
    @PostMapping("/thumbsUp")
    @Operation(summary ="点赞")
    public AppResult thumbsUp(HttpServletRequest request,
                              @Parameter(description = "帖子id") @RequestParam(value = "id") @NonNull Long id){
        //校验用户的状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        articleService.thumbsCountById(id);
        return AppResult.success();
    }
    @Operation(summary = "删除帖子")
    @PostMapping("/deleteById" )
    public AppResult deleteById(HttpServletRequest request,
                                @Parameter(description = "帖子id") @RequestParam(value = "id") @NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验当前用户是否是作者
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //调用service
        articleService.deleteById(id);
        return AppResult.success();
    }


    @Operation(summary = "获取单个用户的帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                               @Parameter(description = "用户id")  @RequestParam (value = "userId",required = false)  Long userId){
        User user=new User();

        if(userId==null){
            HttpSession session = request.getSession(false);
            //获取user对象
            user =(User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        if(user.getId()==userId){

        }
        List<Article> articles = articleService.selectByUserId(userId);
        for(int i=0;i<articles.size();i++){
            if(user.getId()==userId){
                articles.get(i).setIsOwn(true);
            }
        }
        if(articles==null){
            return AppResult.failed("帖子列表不存在");
        }
        return AppResult.success(articles);
    }
    
}

